﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.Attributes;
using KMF.WebUI.Areas.KMFadmin.CustomHelpers;
using KMF.WebUI.Areas.KMFadmin.Infrastructure;
using System.Xml;
using KMF.WebUI.Areas.KMFadmin.Models;
namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    [AdminLoggedAuthorize]
    public class AdminWebInfoController : Controller
    {
        //
        // GET: /WebInfo/

        public ActionResult Index()
        {
            var model = new WebInfoDto();
            try
            {
                model = WebInfoDb.GetAll()[0];
            }
            catch { }
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(WebInfoDto model, HttpPostedFileWrapper imageFile)
        {
            //kiem tra xem co thong tin chua neu chua thi them moi 
            var error = string.Empty;
            var imagePath = string.Empty;
            var listWeb = WebInfoDb.GetAll();
            var folderUpload = Server.MapPath(ConstParam.FolderUploadWebinfoImage);
            if (imageFile != null) imagePath = FunctionHelper.UploadImages(out error, folderUpload, imageFile);
            if (imageFile != null) { model.LogoPath = imagePath; }
            if (listWeb != null && listWeb.Any())
            {
                var idWeb = listWeb[0].Id;
                model.Id = idWeb;
                model.UpdateDate = DateTime.Now;
                WebInfoDb.Update(model);
            }
            else
            {
                model.CreateDate = DateTime.Now;
                model.UpdateDate = DateTime.Now;
                WebInfoDb.Add(model);
            }
            
            return RedirectToAction("Index","AdminWebInfo");
        }

    }
}
